<?php
/**
 * Geneone: Content Management Framework
 * User Namespace
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
class Gene_Namespace_User {
	/**
	 * Calls page in this namespace.
	 *
	 * @static
	 * @var $id string Page ID in Namespace
	 * @var $action array Action Argument String
	 * @return bool Status
	 */
	function call($id, $action=array()) {
		$tpl =& Gene::getTemplate();
		$name = str_replace("_", " ", strtolower($id));
		
		/* The default action is "view" */
		if (!isset($action[1])) {
			$action[1] = "view";
		}
		
		if ($user = Gene_User::fromPublicName($name)) {
			$info = $user->getInfo();

			$actions = Gene_Namespace_User::getAllowedActions($info, $action);
			if (!isset($actions[$action[1]])) {
				return false;
			}
			
			foreach ($actions as $j => $k) {
				if ($k[0] == $action[1]) {
					$actions[$j][2] = true;
				}
			}
			
			$tpl->assign("actions", $actions);
		
			switch ($action[1]) {
				case 'view':
					return Gene_Namespace_User::callView($id);
					break;
				case 'edit':
					return Gene_Namespace_User::callEdit($id);
					break;
			}		
		} else {
			return false;
		}
	}
	
	/**
	 * Call View Method
	 * 
	 * @static
	 * @var $id string Page ID in Namespace
	 * @return bool Status
	 */
	function callView($id) {
		$tpl =& Gene::getTemplate();
		$name = str_replace("_", " ", strtolower($id));
		
		if ($user = Gene_User::fromPublicName($name)) {
			$info = $user->getInfo();
			foreach ($info as $j => $k) {
				if (!$k OR $j == "user_id") {
					unset($info[$j]);
				}
			}
			
			$tpl->append("alternate", array(
				"type" => "application/rdf+xml",
				"title" => 'FOAF Profile for '.$info['name'],
				"url" => Gene::xmlLink(Gene::userID($info['name']))
			));

			$tpl->assign("pagetitle", $info['name']);
			$tpl->assign("profile", $info);
			$tpl->assign("publicnames", $user->getAllPublicNames());
			$tpl->assign("groups", $user->getGroupInfo());
			$tpl->assign("curlbase", Gene::makeLink(Gene::userID($info['name'])));
			$tpl->assign("finalbody", $tpl->fetch("user/viewprofile"));
			
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Call Edit Method
	 * 
	 * @static
	 * @var $id string Page ID in Namespace
	 * @return bool Status
	 */
	function callEdit($id) {
		$tpl =& Gene::getTemplate();
		$auth =& Gene::getAuth();
		$name = str_replace("_", " ", strtolower($id));
		
		if ($auth->requirePasswordRetype()) {
			$returnurl = Gene::userID($id).'/edit';
			$tpl->assign ("returnurl", $returnurl);
			
			require_once 'Gene/Namespace/Special.php';
			Gene_Namespace_Special::call("Retype");
			return true;
		}
		
		if ($user = Gene_User::fromPublicName($name)) {
			$info = $user->getInfo();
			
			$currentuser = Gene::getUser();
			$c = $currentuser->getInfo();
			
			if ($currentuser->hasPermission("namespaces.user.edit_other") OR ($currentuser->hasPermission("namespaces.user.edit_own") && $info['user_id'] == $c['user_id'])) {
				/* Update Profile if we need so */
				if ($_SERVER['REQUEST_METHOD'] == 'POST') {
					$user->setProfile(array(
						"bio" => Gene_Request::getString("profilebio", GENE_REQUEST_POST),
						"gender" => Gene_Request::getString("profilegender", GENE_REQUEST_POST),
						"location" => Gene_Request::getString("profilelocation", GENE_REQUEST_POST),
						"homepage" => Gene_Request::getString("profileurl", GENE_REQUEST_POST),
						"jabber" => Gene_Request::getString("profilejabber", GENE_REQUEST_POST),
					));
					
					$info = $user->getInfo();
					
					$tpl->assign ("success", true);
				}
			
				$tpl->assign("pagetitle", $info['name']);
				$tpl->assign("profile", $info);
				$tpl->assign("curlbase", Gene::makeLink(Gene::userID($info['name'])));
				$tpl->assign("finalbody", $tpl->fetch("user/editprofile"));
			} else {
				return false;
			}
					
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Return allowed actions for Page ID
	 *
	 * @static
	 * @var $userinfo string User Information
	 * @return array Allowed Actions for user
	 */
	function &getAllowedActions(&$userinfo) {	
		$user =& Gene::getUser();
		$info = $user->getInfo();
		
		if ($user->hasPermission("namespaces.user.edit_other") OR ($user->hasPermission("namespaces.user.edit_own") && $userinfo['user_id'] == $info['user_id'])) {
			$actions = array("view" => array("view", "View"), "edit" => array("edit", "Edit"));
		} else {
			$actions = array("view" => array("view", "View"));
		}
		
		return $actions;
	}
	
	/**
	 * Exports document as XML.
	 *
	 * @var $id string Page ID
	 *
	 * @return boolean Status
	 */
	function exportXml($id) {
		if ($user =& Gene_User::fromPublicName($id)) {
			header ("Content-Type: application/rdf+xml");
			
			$tree =& new XML_Tree();

			// <rdf:RDF> root tag
			$namespaces = array(
				 "xmlns:rdf" => "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
				 "xmlns:rdfs" => "http://www.w3.org/2000/01/rdf-schema#",
				 "xmlns:foaf" => "http://xmlns.com/foaf/0.1/",
				 "xmlns:profile" => "http://geneone.neondragon.net/rdf/member",
				 "xmlns" => "http://xmlns.com/foaf/0.1/",
			);

			$root =& $tree->addRoot("rdf:RDF", "", $namespaces);

			// Person
			$person =& $root->addChild("foaf:Person", "", array("rdf:nodeID" => "me"));

			// Get person details
			$info = $user->getInfo();

			// Add this persons stuff
			$details = array("foaf:nick" => $info['name']);

			// "FOAF Core" Profile Elements
			if ($info['name']) { $details['foaf:name'] = $info['name']; }
			if ($info['gender']) { $details['foaf:gender'] = ($info['gender'] == "m") ? "male" : "female"; }
			if ($info['homepage']) { $details['foaf:homepage'] = $info['homepage']; }
			if ($info['jabber']) { $details['foaf:jabberID'] = $info['jabber']; }

			// Gene "Profile" elements. Semantically, these may not be ideal uses of RDF
			// but they suit our purposes as a good way to transfer member data
			if ($info['location']) { $details['profile:location'] = $info['location']; }
			if ($info['bio']) { $details['profile:bio'] = $info['bio']; }

			foreach ($details as $j => $k) {
				$person->addChild($j, $k);
			}

			// Account on this community
			$accounts =& $person->addChild("foaf:holdsAccount");
			$community =& $accounts->addChild("foaf:OnlineAccount");

			$community->addChild("foaf:accountServiceHomepage", Gene::getSetting("gene_uri"));
			$community->addChild("foaf:accountName", $info['name']);

			$tree->dump();
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * Runs web services.
	 *
	 * @var $method string Method to Call
	 * @return array/bool Result
	 */
	function runWebServices($method) {
		switch ($method) {
			case 'checkname':
				$name = Gene_Request::getString("regname", GENE_REQUEST_POST);
				$name = Gene_User::canonizePublicName($name);
				if (Gene_User::fromPublicName($name)) {
					$taken = true;
				} else {
					$taken = false;
				}
				
				return array("name" => $name, "taken" => $taken);
				
				break;
		}
		return false;
	}
}

?>